<script setup>
import { ref } from 'vue'
import { userInfoService, userpasswordUpdateService } from '@/api/user.js'
import { ElMessage, ElMessageBox } from 'element-plus'
//使用路由，导出useRouter函数
import { useRouter } from 'vue-router';
const router = useRouter()

import { useUserInfoStore } from '@/stores/userInfo.js'
const userInfoStore = useUserInfoStore();
const info = ref({
    id: 0,
    oldPassword: '',
    newPassword: '',
    rePassword: ''
})
//获取个人信息
const getUserInfo = async () => {
    let result = await userInfoService();
    info.value.id = result.data.id;
}
getUserInfo();
//自定义确认密码的校验函数
const rePasswordValid = (rule, value, callback) => {
  // 正则表达式，用于检查密码是否包含数字、字母和字符，且长度大于7位


  if (value === '') {
    callback(new Error('请再次输入密码'));
  } else {
    
    callback();
  }
};

//用于注册的表单校验模型
const registerDataRules = ref({
    oldPassword: [
        { required: true, message: '请输入原密码', trigger: 'blur' },
        { min: 5, max: 16, message: '用户名的长度必须为5~16位', trigger: 'blur' }
    ],
    newPassword: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, message: '密码长度必须大于等于6位', trigger: 'blur' },
        { validator: rePasswordValid, trigger: 'blur' }
    ],
    rePassword: [
        { required: true, validator: rePasswordValid, trigger: 'blur' }
    ]

})
const updata = async () => {
    let result = await userpasswordUpdateService(info.value);
    console.log(info.value);
    if (result.code == 0) {
        ElMessage.success("修改密码成功，请重新登录");
        userInfoStore.info = {}
        //跳转到登录页
        router.push('/login')
    }
    else {
        ElMessage.error(result.message ? result.message : "修改失败");
    }
}




</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>重置密码</span>
            </div>
        </template>
        <el-row>
            <el-col :span="12">
                <el-form :model="info" :rules="registerDataRules" label-width="100px" size="large">
                    <el-form-item label="原密码" prop="oldPassword">
                        <el-input v-model="info.oldPassword" type="password" placeholder="请输入原密码"></el-input>
                    </el-form-item>
                    <el-form-item label="新密码" prop="newPassword">
                        <el-input v-model="info.newPassword" type="password" placeholder="请输入新密码"></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码" prop="rePassword">
                        <el-input v-model="info.rePassword" type="password" placeholder="请再次输入新密码"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="updata">确认修改</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </el-card>
</template>




getUserInfo();

userInfoUpdate();
